/*
 * Created by JFormDesigner on Thu Nov 11 14:59:23 CET 2010
 */

package lp.chess.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;
import javax.swing.border.*;

import lp.chess.Chess;

/**
 * @author Antoine Domenger
 */
public class MainForm extends JPanel {
	public MainForm() {
		initComponents();
		initGraphicChessboard();
		initListeners();
		initMnemonics();
		
		// Comment next line to enable the chat panel display:
		disableChat();
		// Comment next line to enable the timer display:
		disableTimer();
	}
	
	private void disableChat() {
		panelChat.setVisible(false);
	}
	
	private void disableTimer() {
		panelTimer.setVisible(false);
	}
	
	private void initMnemonics() {
		menuFile.setMnemonic('F');
			menuItemNew.setMnemonic('N');
			menuItemExit.setMnemonic('X');
			
		menuHelp.setMnemonic('H');
			menuItemAbout.setMnemonic('A');
	}
	
	private void initListeners() {
		
		// File menu:
		menuItemNew.addActionListener(new MenuActionListener());
		menuItemExit.addActionListener(new MenuActionListener());
		
		// Help menu:
		menuItemAbout.addActionListener(new MenuActionListener());
		
		// Control buttons:
		buttonFirst.addActionListener(new ButtonActionListener());
		buttonBack.addActionListener(new ButtonActionListener());
		buttonNext.addActionListener(new ButtonActionListener());
		buttonLast.addActionListener(new ButtonActionListener());
	}
	
	private void initGraphicChessboard() {
		panelGameVIew.add(Chess.getGraphicChessboard().getJCanvas());
	}
	
	public void popupDialog(String text) {
		JOptionPane.showMessageDialog(this, text);
	}
	
	public void updateTurn(int turn) {
		textFieldTurn.setText(""+turn);
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		// Generated using JFormDesigner non-commercial license
		menuBarMain = new JMenuBar();
		menuFile = new JMenu();
		menuItemNew = new JMenuItem();
		menuItemExit = new JMenuItem();
		menuHelp = new JMenu();
		menuItemAbout = new JMenuItem();
		panelMain = new JPanel();
		panelGame = new JPanel();
		panelGameVIew = new JPanel();
		panelControl = new JPanel();
		panelTurn = new JPanel();
		buttonFirst = new JButton();
		buttonBack = new JButton();
		textFieldTurn = new JTextField();
		buttonNext = new JButton();
		buttonLast = new JButton();
		panelTimer = new JPanel();
		labelTimerText = new JLabel();
		labelTimer = new JLabel();
		panelChat = new JPanel();
		scrollPaneChat = new JScrollPane();
		textPane1 = new JTextPane();
		panelChatInput = new JPanel();
		textFieldChat = new JTextField();
		buttonChat = new JButton();

		//======== this ========
		setLayout(new GridBagLayout());
		((GridBagLayout)getLayout()).columnWidths = new int[] {0, 0};
		((GridBagLayout)getLayout()).rowHeights = new int[] {0, 0, 0};
		((GridBagLayout)getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
		((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 1.0, 1.0E-4};

		//======== menuBarMain ========
		{

			//======== menuFile ========
			{
				menuFile.setText("File");

				//---- menuItemNew ----
				menuItemNew.setText("New");
				menuFile.add(menuItemNew);

				//---- menuItemExit ----
				menuItemExit.setText("Exit");
				menuFile.add(menuItemExit);
			}
			menuBarMain.add(menuFile);

			//======== menuHelp ========
			{
				menuHelp.setText("?");

				//---- menuItemAbout ----
				menuItemAbout.setText("About");
				menuHelp.add(menuItemAbout);
			}
			menuBarMain.add(menuHelp);
		}
		add(menuBarMain, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
			GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
			new Insets(0, 0, 5, 0), 0, 0));

		//======== panelMain ========
		{
			panelMain.setLayout(new GridBagLayout());
			((GridBagLayout)panelMain.getLayout()).columnWidths = new int[] {0, 0, 0};
			((GridBagLayout)panelMain.getLayout()).rowHeights = new int[] {0, 0};
			((GridBagLayout)panelMain.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0E-4};
			((GridBagLayout)panelMain.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};

			//======== panelGame ========
			{
				panelGame.setLayout(new GridBagLayout());
				((GridBagLayout)panelGame.getLayout()).columnWidths = new int[] {512, 0};
				((GridBagLayout)panelGame.getLayout()).rowHeights = new int[] {517, 0, 0};
				((GridBagLayout)panelGame.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};
				((GridBagLayout)panelGame.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0E-4};

				//======== panelGameVIew ========
				{
					panelGameVIew.setBorder(new TitledBorder("Game"));
					panelGameVIew.setLayout(new GridBagLayout());
					((GridBagLayout)panelGameVIew.getLayout()).columnWidths = new int[] {0, 0};
					((GridBagLayout)panelGameVIew.getLayout()).rowHeights = new int[] {0, 0};
					((GridBagLayout)panelGameVIew.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
					((GridBagLayout)panelGameVIew.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};
				}
				panelGame.add(panelGameVIew, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 5, 0), 0, 0));

				//======== panelControl ========
				{
					panelControl.setBorder(new TitledBorder("Controls"));
					panelControl.setLayout(new GridBagLayout());
					((GridBagLayout)panelControl.getLayout()).columnWidths = new int[] {0, 0, 0, 0};
					((GridBagLayout)panelControl.getLayout()).rowHeights = new int[] {0, 0};
					((GridBagLayout)panelControl.getLayout()).columnWeights = new double[] {0.0, 1.0, 0.0, 1.0E-4};
					((GridBagLayout)panelControl.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

					//======== panelTurn ========
					{
						panelTurn.setLayout(new GridBagLayout());
						((GridBagLayout)panelTurn.getLayout()).columnWidths = new int[] {0, 0, 40, 0, 0, 0};
						((GridBagLayout)panelTurn.getLayout()).rowHeights = new int[] {0, 0};
						((GridBagLayout)panelTurn.getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
						((GridBagLayout)panelTurn.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};

						//---- buttonFirst ----
						buttonFirst.setText("|<");
						panelTurn.add(buttonFirst, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 5), 0, 0));

						//---- buttonBack ----
						buttonBack.setText("<");
						panelTurn.add(buttonBack, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 5), 0, 0));

						//---- textFieldTurn ----
						textFieldTurn.setText("0");
						textFieldTurn.setHorizontalAlignment(SwingConstants.CENTER);
						textFieldTurn.setEditable(false);
						textFieldTurn.setFont(textFieldTurn.getFont().deriveFont(textFieldTurn.getFont().getStyle() & ~Font.BOLD));
						panelTurn.add(textFieldTurn, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 5), 0, 0));

						//---- buttonNext ----
						buttonNext.setText(">");
						panelTurn.add(buttonNext, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 5), 0, 0));

						//---- buttonLast ----
						buttonLast.setText(">|");
						panelTurn.add(buttonLast, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 0), 0, 0));
					}
					panelControl.add(panelTurn, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 5), 0, 0));

					//======== panelTimer ========
					{
						panelTimer.setLayout(new GridBagLayout());
						((GridBagLayout)panelTimer.getLayout()).columnWidths = new int[] {0, 0, 0};
						((GridBagLayout)panelTimer.getLayout()).rowHeights = new int[] {0, 0};
						((GridBagLayout)panelTimer.getLayout()).columnWeights = new double[] {0.0, 0.0, 1.0E-4};
						((GridBagLayout)panelTimer.getLayout()).rowWeights = new double[] {1.0, 1.0E-4};

						//---- labelTimerText ----
						labelTimerText.setText("Time elapsed:");
						panelTimer.add(labelTimerText, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 5), 0, 0));

						//---- labelTimer ----
						labelTimer.setText("00:00");
						labelTimer.setHorizontalAlignment(SwingConstants.CENTER);
						panelTimer.add(labelTimer, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
							GridBagConstraints.CENTER, GridBagConstraints.BOTH,
							new Insets(0, 0, 0, 0), 0, 0));
					}
					panelControl.add(panelTimer, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));
				}
				panelGame.add(panelControl, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			panelMain.add(panelGame, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 5), 0, 0));

			//======== panelChat ========
			{
				panelChat.setBorder(new TitledBorder("Chat"));
				panelChat.setLayout(new GridBagLayout());
				((GridBagLayout)panelChat.getLayout()).columnWidths = new int[] {200, 0};
				((GridBagLayout)panelChat.getLayout()).rowHeights = new int[] {0, 0, 0};
				((GridBagLayout)panelChat.getLayout()).columnWeights = new double[] {1.0, 1.0E-4};
				((GridBagLayout)panelChat.getLayout()).rowWeights = new double[] {1.0, 0.0, 1.0E-4};

				//======== scrollPaneChat ========
				{
					scrollPaneChat.setViewportView(textPane1);
				}
				panelChat.add(scrollPaneChat, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 5, 0), 0, 0));

				//======== panelChatInput ========
				{
					panelChatInput.setLayout(new GridBagLayout());
					((GridBagLayout)panelChatInput.getLayout()).columnWidths = new int[] {0, 0, 0};
					((GridBagLayout)panelChatInput.getLayout()).rowHeights = new int[] {0, 0};
					((GridBagLayout)panelChatInput.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0E-4};
					((GridBagLayout)panelChatInput.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};
					panelChatInput.add(textFieldChat, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 5), 0, 0));

					//---- buttonChat ----
					buttonChat.setText("Send");
					panelChatInput.add(buttonChat, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
						GridBagConstraints.CENTER, GridBagConstraints.BOTH,
						new Insets(0, 0, 0, 0), 0, 0));
				}
				panelChat.add(panelChatInput, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH,
					new Insets(0, 0, 0, 0), 0, 0));
			}
			panelMain.add(panelChat, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 0), 0, 0));
		}
		add(panelMain, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
			GridBagConstraints.CENTER, GridBagConstraints.BOTH,
			new Insets(5, 5, 5, 5), 0, 0));
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	// Generated using JFormDesigner non-commercial license
	private JMenuBar menuBarMain;
	private JMenu menuFile;
	private JMenuItem menuItemNew;
	private JMenuItem menuItemExit;
	private JMenu menuHelp;
	private JMenuItem menuItemAbout;
	private JPanel panelMain;
	private JPanel panelGame;
	private JPanel panelGameVIew;
	private JPanel panelControl;
	private JPanel panelTurn;
	private JButton buttonFirst;
	private JButton buttonBack;
	private JTextField textFieldTurn;
	private JButton buttonNext;
	private JButton buttonLast;
	private JPanel panelTimer;
	private JLabel labelTimerText;
	private JLabel labelTimer;
	private JPanel panelChat;
	private JScrollPane scrollPaneChat;
	private JTextPane textPane1;
	private JPanel panelChatInput;
	private JTextField textFieldChat;
	private JButton buttonChat;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
	
	class MenuActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// File menu:
			if(e.getSource() == menuItemNew) {
				Chess.getGraphicChessboard().restart();
			}
			
			if(e.getSource() == menuItemExit) {
				System.exit(0);
			}
			
			// Help menu:
			if(e.getSource() == menuItemAbout) {
				Chess.getMainForm().popupDialog("This game of chess was made during a student project at IUT de Bordeaux 1\nby Brian Boadi and Antoine Domenger in 2010.");
			}
		}
	}
	
	class ButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// File menu:
			if(e.getSource() == buttonFirst) {
				Chess.getGraphicChessboard().first();
			}
			
			if(e.getSource() == buttonBack) {
				Chess.getGraphicChessboard().back();
			}
			
			if(e.getSource() == buttonNext) {
				Chess.getGraphicChessboard().next();
			}
			
			if(e.getSource() == buttonLast) {
				Chess.getGraphicChessboard().last();
			}
		}
	}
}
